import {
	productionPrdNoticeMachineChangeBatchClearRenovation,
	productionPrdNoticeMachineChangeGetStateCount,
	productionPrdNoticeMachineChangePostPage, productionPrdNoticeMachineCloseBatchClearClose,
	productionPrdNoticeMachineOpeningBatchClearOpening
} from '../../../../../api/production/xsRenovateOrder';
import { openStateList, orderPriorityList } from '../../../../../utils/jk/status-option-list';
import { pageConfig } from '../../../../../utils/jk/jk-tool';

export default {
	data() {
		return {
			drawerState: false,
			batchBtnSize: 'small',
			size: 'large',
			prefix: '',
			apiItem: {
				'page': productionPrdNoticeMachineChangePostPage
			},
			isBatch: false,
			moreSearchParams: {
				productOrderCode: '',
				code: '',
				prdNoticeCode: '',
				batchCode: '',
				priorityId: null
			},
			batchOpenDialogState: false,
			batchCloseDialogState: false,
			openAgainDialogState: false,
			closeDialogState: false,
			openDialogState: false,
			selectMachineItem: {},
			workshopList: [],
			openStateList: openStateList.filter(x => x.id !== 0),
			tableLoading: true,
			checkArr: [],
			totalCount: 0,
			timer: {},
			priorityList: orderPriorityList,
			searchParams: {
				pageNo: pageConfig.pageNo,
				pageSize: pageConfig.pageSize,
				machineNameOrCode: '',
				productNameOrCode: '',
				ifTest: false,
				processCodes: [],
				batchCode: '',
				openingState: 3,
				workshopId: null
			},
			smList: [],
			btList: [],
			csList: [],
			xsList: [],
			btProcessCodes: ['BT', 'TB', 'EB', 'MB'],
			checkProcessArr: []
		};
	},
	methods: {
		onOpeningStateChange() {
			this.checkArr = [];
			this.searchParams.pageNo = 1;
			this.getListRequest();
		},
		onBatchUnRenovationBtn(processCodes) {
			this.checkProcessArr = this.checkArr.filter(x => processCodes.includes(x.processCode.toUpperCase()));
			this.$confirm(`确定"取消翻改"吗？( ${this.checkProcessArr.map(x => x.id)} )`, '提示', { type: 'warning' }).then(() => {
				productionPrdNoticeMachineChangeBatchClearRenovation(this.checkProcessArr.map(x => x.id)).then(res => {
					if (res.data.status === 200) {
						this.getListRequest();
					}
				});
			}).catch(() => {});
		},
		onBatchUnOpenBtn(processCodes) {
			this.checkProcessArr = this.checkArr.filter(x => processCodes.includes(x.processCode.toUpperCase()));
			this.$confirm(`确定"取消开台"吗？( ${this.checkProcessArr.map(x => x.id)} )`, '提示', { type: 'warning' }).then(() => {
				productionPrdNoticeMachineOpeningBatchClearOpening(this.checkProcessArr.map(x => x.id)).then(res => {
					if (res.data.status === 200) {
						this.getListRequest();
					}
				});
			}).catch(() => {});
		},
		onBatchUnCloseBtn(processCodes) {
			this.checkProcessArr = this.checkArr.filter(x => processCodes.includes(x.processCode.toUpperCase()));
			this.$confirm(`确定"取消了机"吗？( ${this.checkProcessArr.map(x => x.id)} )`, '提示', { type: 'warning' }).then(() => {
				productionPrdNoticeMachineCloseBatchClearClose(this.checkProcessArr.map(x => x.id)).then(res => {
					if (res.data.status === 200) {
						this.getListRequest();
					}
				});
			}).catch(() => {});
		},
		getProcessCheckCount(processCodes) {
			return this.checkArr.filter(x => processCodes.includes(x.processCode.toUpperCase())).length || 0;
		},
		onToggleBatch() {
			this.isBatch = !this.isBatch;
			if (!this.isBatch) {
				this.checkArr = [];
			}
		},
		isCheck(item) {
			return this.checkArr.some(x => x.id === item.id);
		},
		onJumpBtn(name) {
			this.$router.push({
				name: name
			});
		},
		onBackBtn() {
			this.$toMainRouter({
				name: 'ProductOpenCloseMachine'
			});
		},
		onClickBtn(item) {
			if (this.isBatch) {
				if (this.checkArr.some(x => x.id === item.id)) {
					this.checkArr = this.checkArr.filter(x => x.id !== item.id);
				} else {
					this.checkArr.push(item);
				}
			} else {
				if ([1, 2].includes(item.openingState)) {
					this.onOpenBtn(item);
				} else if (item.openingState === 3) {
					this.onCloseBtn(item);
				}
			}
		},
		onConfirmChangeOfBatchCloseDialog() {
			this.batchCloseDialogState = false;
			this.getListRequest();
		},
		onBatchCloseBtn(processCodes) {
			this.checkProcessArr = this.checkArr.filter(x => processCodes.includes(x.processCode.toUpperCase()));
			this.batchCloseDialogState = true;
		},
		onVisibleChangeOfBatchCloseDialog(e) {
			this.batchCloseDialogState = e;
		},
		onBatchOpenBtn(processCodes) {
			this.checkProcessArr = this.checkArr.filter(x => processCodes.includes(x.processCode.toUpperCase()));
			this.batchOpenDialogState = true;
		},
		onConfirmChangeOfBatchOpenDialog() {
			this.batchOpenDialogState = false;
			this.getListRequest();
		},
		onVisibleChangeOfBatchOpenDialog(e) {
			this.batchOpenDialogState = e;
		},
		onCheckBoxChange({ records }) {
			this.checkArr = records.filter(x => !x._disabled).map(x => {
				return x;
			});
		},
		onOpenBtn(row) {
			this.selectMachineItem = row;
			this.openDialogState = true;
		},
		onCloseBtn(row) {
			this.selectMachineItem = row;
			this.closeDialogState = true;
		},
		onConfirmChangeOfOpenAgainDialog() {
			this.openAgainDialogState = false;
			this.getListRequest();
		},
		onVisibleChangeOfOpenAgainDialog(e) {
			this.openAgainDialogState = e;
		},
		onVisibleChangeOfCloseDialog(e) {
			this.closeDialogState = e;
		},
		onConfirmChangeOfCloseDialog() {
			this.closeDialogState = false;
			this.getListRequest();
		},
		onConfirmChangeOfOpenDialog() {
			this.openDialogState = false;
			this.getListRequest();
		},
		onVisibleChangeOfOpenDialog(e) {
			this.openDialogState = e;
		},
		onResetSearchBtn() {
			Object.assign(this.$data.moreSearchParams, this.$options.data().moreSearchParams);
		},
		onPageChange(pageIndex, pageSize) {
			this.searchParams.pageNo = pageIndex;
			this.searchParams.pageSize = pageSize;
			this.getListRequest();
		},
		getStateBarCountRequest(params) {
			productionPrdNoticeMachineChangeGetStateCount({
				workshopId: params.workshopId,
				processCodes: params.processCodes
			}).then(res => {
				if (res.data.status === 200) {
					this.openStateList = this.openStateList.map(x => {
						x.count = 0;
						res.data.res.forEach(k => {
							if (x.id === k.openingState) {
								x.count = k.count;
							}
						});
						return x;
					});
				} else {
					this.openStateList = this.openStateList.map(x => {
						x.count = 0;
						return x;
					});
				}
			});
		},
		/** 搜索按钮操作 */
		onSearchBtn() {
			this.searchParams.pageNo = 1;
			this.totalCount = 1;
			this.getListRequest();
		},
		clearCacheMethod() {
			// Object.assign(this.$data, this.$options.data());
			this.$nextTick(() => {
				this.runData();
				this.getDependentData();
			});
		},
		runData() {
			this.getDependentData();
			this.clearTimer();
			this.timer = setInterval(() => {
				this.getDependentData();
			}, 1000 * 60 * 5); // 间隔5分钟
		},
		clearTimer() {
			clearInterval(this.timer);
		},
		async getDependentData() {
			this.workshopList = this.$store.state.user.userWorkshop.workshopList;
			await this.getListRequest();
		}
	},
	mounted() {
	},
	beforeRouteEnter(to, from, next) {
		next((_this) => {
			if (!_this.searchParams.workshopId) {
				_this.$set(_this.searchParams, 'workshopId', _this.$store.state.user.userWorkshop.defaultWorkshopId);
			}
			_this.runData();
		});
	},
	beforeRouteLeave(to, from, next) {
		this.clearTimer();
		next();
	}
};
